import { Button, Component, EventHandler, EventTouch, Node, UIOpacity, UIRenderer, UITransform, Vec2, Vec3, math } from "cc";

let Polyfill = {
    init() {
        if (window["polyfillInited"]) return;
        window["polyfillInited"] = true;

        //节点添加组件（如果有则返回，没有则添加）
        Object.defineProperty(Node.prototype, "getOrAddComponentEx", {
            value: function (component: Component) {
                let ret = this.getComponent(component);
                if (ret == null) {
                    ret = this.addComponent(component);
                }
                return ret;
            }
        })

        Object.defineProperty(Node.prototype, "scaleEx", {
            get: function () {
                return this.scale.x
            },
            set: function (val: number) {
                this.scale = new Vec3(val, val, 1) // 第三位是1
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "scaleXEx", {
            get: function () {
                return this.scale.x
            },
            set: function (val: number) {
                this.scale = new Vec3(val, this.scale.y, 1) // 第三位是1
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "scaleYEx", {
            get: function () {
                return this.scale.y
            },
            set: function (val: number) {
                this.scale = new Vec3(this.scale.x, val, 1) // 第三位是1
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "xEx", {
            get: function () {
                return this.getPosition().x
            },
            set: function (val: number) {
                this.setPosition(val, this.getPosition().y, 0) // 第三位是0
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "yEx", {
            get: function () {
                return this.getPosition().y
            },
            set: function (val: number) {
                this.setPosition(this.getPosition().x, val, 0) // 第三位是0
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "widthEx", {
            get: function () {
                return this.getOrAddComponentEx(UITransform).width
            },
            set: function (val: number) {
                let uiTransform = this.getOrAddComponentEx(UITransform)
                uiTransform.width = val
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "heightEx", {
            get: function () {
                return this.getOrAddComponentEx(UITransform).height
            },
            set: function (val: number) {
                let uiTransform = this.getOrAddComponentEx(UITransform)
                uiTransform.height = val
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "setContentSizeEx", {
            value: function (size: number | math.Size, height?: number) {
                this.getOrAddComponentEx(UITransform).setContentSize(size, height)
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "anchorEx", {
            get: function (): Vec2 {
                return this.getOrAddComponentEx(UITransform).anchorPoint
            },
            set: function (val: Vec2) {
                let uiTransform = this.getOrAddComponentEx(UITransform)
                uiTransform.anchorPoint = val
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "anchorXEx", {
            get: function () {
                return this.getOrAddComponentEx(UITransform).anchorX
            },
            set: function (val: number) {
                let uiTransform = this.getOrAddComponentEx(UITransform)
                uiTransform.anchorX = val
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "anchorYEx", {
            get: function () {
                return this.getOrAddComponentEx(UITransform).anchorY
            },
            set: function (val: number) {
                let uiTransform = this.getOrAddComponentEx(UITransform)
                uiTransform.anchorY = val
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "setAnchorPointEx", {
            value: function (point: math.Vec2 | Readonly<math.Vec2> | number, y?: number) {
                return this.getOrAddComponentEx(UITransform).setAnchorPoint(point, y)
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "positionEx", {
            get: function () {
                return this.getPosition()
            },
            set: function (val: math.Vec3 | math.Vec2) {
                this.setPosition(val.x, val.y)
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "opacityEx", {
            get: function () {
                return this.getOrAddComponentEx(UIOpacity).opacity
            },
            set: function (val: number) {
                this.getOrAddComponentEx(UIOpacity).opacity = val
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "colorEx", {
            get: function () {
                return this.getOrAddComponentEx(UIRenderer).color
            },
            set: function (val: math.Color) {
                this.getOrAddComponentEx(UIRenderer).color = val
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "convertToNodeSpaceAREx", {
            value: function (worldPoint: math.Vec3, out?: math.Vec3) {
                return this.getOrAddComponentEx(UITransform).convertToNodeSpaceAR(worldPoint, out)
            },
            configurable: true,
        })

        Object.defineProperty(Node.prototype, "convertToWorldSpaceAREx", {
            value: function (worldPoint: math.Vec3, out?: math.Vec3) {
                return this.getOrAddComponentEx(UITransform).convertToWorldSpaceAR(worldPoint, out)
            },
            configurable: true,
        })

        // Object.defineProperty(Button.prototype, "_onTouchEnded", {
        //     value: function (event?: EventTouch) {
        //         if (!this._interactable || !this.enabledInHierarchy) {
        //             return;
        //         }

        //         if (this._pressed) {
        //             let buttonEffect = this.getComponent(ButtonEffect)
        //             if (buttonEffect) {
        //                 buttonEffect.playSound()
        //             } else {
        //                 AudioManager.playSound(EFFECT.BUTTON_CLICK);
        //             }
        //             EventManager.emit(GameEvent.ON_BUTTON_CLICK);
        //             // console.log("click");

        //             EventHandler.emitEvents(this.clickEvents, event);
        //             this.node.emit(Button.EventType.CLICK, this);
        //         }
        //         this._pressed = false;
        //         this._updateState();

        //         if (event) {
        //             event.propagationStopped = true;
        //         }
        //     },
        // })
    }
}
export default Polyfill